﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sales_DrawPrint1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                ZSql zsql = new ZSql();
                ZSql count = new ZSql();
                ZSql userSql = new ZSql();
                //购置清单
                string sql = "SELECT a.Brand, a.comodName, a.modul, a.Unit, a.userId, a.number, a.Prices, a.Gifts,a.TotalMoney, CONVERT(varchar(10), a.DeliveryDate, 120) AS ddate, a.SalesContent, CONVERT(varchar(10), a.C_date, 120) AS date,(SELECT phone FROM CS_Supplier WHERE SupplierId =(SELECT TOP 1 SupplierId  FROM dbo.CS_Stock  WHERE ComodBarcode = a.ComodBarcode)) AS phone FROM CS_Sales a WHERE a.SalesCode = '" + Request.QueryString["id"].ToString() + "'";
                //统计信息表
                count.Open("select sum(number)as num,sum(TotalMoney)as price,sum(TotalPractical)as TotalPractical from CS_Sales where SalesCode='" + Request.QueryString["id"].ToString() + "' group by SalesCode");
                //用户信息表
                userSql.Open("select top 1 Saleman, b.GuestName, b.GuestMan, b.Phone, b.Address, (select StaffName from CS_Staff where  a.Saleman = StaffId) as StaffName, (select realname from userInfo where  a.userid = userid) as realname from CS_Sales a,CS_Guest b where SalesCode='" + Request.QueryString["id"].ToString() + "' and b.Guestid=a.guestid");
                zsql.Open(sql);
                DataTable dt = new DataTable();
                string strTable = "";
                dt = zsql.m_table;
                int a = 0;
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        strTable += "<table width=\"97%\"  border=\"0\"><tr><td class=\"F_size18 F_B\" align=\"center\">重庆市南川区协和家电有限公司</td></tr></table>";
                        strTable += "<table width=\"97%\" height=30 border=\"0\" align=\"center\"><tr><td width=\"43%\">&nbsp;</td><td  class=\"F_size16 F_B \">商品销售单</td><td  align=\"right\"></td></tr></table>";

                        strTable += "<table width=\"97%\" align=\"center\" ><tr><td align=\"left\" width=\"33%\">用户姓名：" + userSql.m_table.Rows[0]["GuestMan"].ToString() + "</td><td align=\"left\" width=\"33%\">购买日期：" + zsql.m_table.Rows[0]["date"].ToString() + "</td><td align=\"left\">联系电话：" + userSql.m_table.Rows[0]["phone"].ToString() + "</td></tr><tr><td align=\"left\"  width=\"66%\" colspan=2 >联系地址：" + userSql.m_table.Rows[0]["address"].ToString() + "</td><td align=\"left\" class=nobr >送货日期：" + zsql.m_table.Rows[0]["ddate"].ToString() + "</td></tr></table>";

                        strTable += "<table width=\"97%\" align=\"center\" border=\"1\"><tr><td align=\"center\">品牌</td><td align=\"center\">商品名称</td><td align=\"center\">规格及型号</td><td align=\"center\">数量</td><td align=\"center\">单位</td><td align=\"center\">单价</td><td align=\"center\">金额</td><td>备注</td></tr>";

                        for (int i = 0; i < zsql.m_table.Rows.Count; i++)
                        {
                            strTable += "<tr><td align=\"center\">" + zsql.m_table.Rows[i]["brand"].ToString() + "</td><td align=\"center\">" + zsql.m_table.Rows[i]["comodName"].ToString() + "</td><td align=\"center\">" + zsql.m_table.Rows[i]["modul"].ToString() + "</td><td align=\"center\">" + zsql.m_table.Rows[i]["number"].ToString() + "</td><td align=\"center\">" + zsql.m_table.Rows[i]["unit"].ToString() + "</td><td align=\"right\">" + zsql.m_table.Rows[i]["prices"].ToString().Substring(0, zsql.m_table.Rows[i]["prices"].ToString().Length - 2) + "</td><td align=\"right\">" + zsql.m_table.Rows[i]["TotalMoney"].ToString().Substring(0, zsql.m_table.Rows[i]["TotalMoney"].ToString().Length - 2) + "</td><td align=\"center\">" + zsql.m_table.Rows[i]["phone"].ToString() + "</td></tr>";
                            a++;
                        }

                        for (; a < 6; a++)//补充表格
                        {
                            strTable += "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
                        }

                        strTable += " <tr><th colspan=\"2\">本单合计</th><td colspan=\"3\">" + count.m_table.Rows[0]["num"].ToString() + "</td><td colspan=\"3\" align=\"left\">" + count.m_table.Rows[0]["price"].ToString().Substring(0, count.m_table.Rows[0]["price"].ToString().Length - 2) + "</td></tr>";
                        //如实收(TotalPractical) < 应收(price),则显示出来,并加备注
                        if (Convert.ToInt16(count.m_table.Rows[0]["TotalPractical"]) < Convert.ToInt16(count.m_table.Rows[0]["price"]))
                        {
                            strTable += " <tr><th colspan=\"2\">同意实收</th><td colspan=\"3\">" + count.m_table.Rows[0]["num"].ToString() + "</td><td colspan=\"3\" align=\"left\">" + count.m_table.Rows[0]["TotalPractical"].ToString().Substring(0, count.m_table.Rows[0]["TotalPractical"].ToString().Length - 2) + "(盖章有效)</td></tr>";
                        }
                        strTable += "</table><table width=\"97%\" border=\"0\" align=\"center\"><tr><td colspan=\"3\"><font color=\"#FF0000\"><p>提示：请您仔细核对商品型号、数量、金额是否一致。白色存根联、红色用户联</p></font></td></tr><tr  height=22><td  align=\"left\">销售员：" + userSql.m_table.Rows[0]["StaffName"].ToString() + "</td><td  align=\"left\">收银员：" + userSql.m_table.Rows[0]["realname"].ToString() + "</td><td  align=\"left\" width=\"45%\" >备注：" + zsql.m_table.Rows[0]["SalesContent"].ToString() + "</td></tr></table>";


                    }
                }
                zsql.Close();
                count.Close();
                userSql.Close();

                lblTable.Text = strTable;
            }
        }
    }
}
